//
//  ViewController.m
//  09-tableView-ProvinceCityZone
//
//  Created by 402030900@qq.com on 16/7/26.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import "ViewController.h"
#import "CityTableViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableArray * provinceArray;
@property(nonatomic,strong)NSMutableArray * cityArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _provinceArray = [NSMutableArray array];
    _cityArray = [NSMutableArray array];
    self.navigationItem.title = @"省市区";
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    NSString * path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"txt"];
    NSString * allStr =[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray * allArray = [allStr componentsSeparatedByString:@"\n"];
    NSMutableArray * array = [NSMutableArray array];
    
    for (NSString * temp in allArray) {
        if (![temp hasPrefix:@" "]) {
            NSMutableDictionary * provinceDic = [NSMutableDictionary dictionary];
            [provinceDic setObject:temp forKey:@"proName"];
            [array addObject:provinceDic];
            NSMutableArray * cityArr = [NSMutableArray array];
            [provinceDic setObject:cityArr forKey:@"cityArray"];
        }else if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "]){
            NSMutableDictionary * provinceDic = [array lastObject];
            NSMutableArray * cityArr = [provinceDic objectForKey:@"cityArray"];
            NSMutableDictionary * cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:temp forKey:@"cityName"];
            [cityArr addObject:cityDic];
            NSMutableArray * zoneArr = [NSMutableArray array];
            [cityDic setObject:zoneArr forKey:@"zoneArray"];
        }else if([temp hasPrefix:@"    "]){
            NSMutableDictionary * proDic = [array lastObject];
            NSMutableArray * cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary * cityDic = [cityArray lastObject];
            NSMutableArray * zoneArr = [cityDic objectForKey:@"zoneArray"];
            [zoneArr addObject:temp];
           
        }
//        NSLog(@"%@",array);
    }
    for (NSDictionary * dic in array) {
        [self.provinceArray addObject:[dic objectForKey:@"proName"]];
        [self.cityArray addObject:[dic objectForKey:@"cityArray"]];
    }
    
    
    
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _provinceArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
    cell.textLabel.text = _provinceArray[indexPath.row];
    cell.detailTextLabel.text =_provinceArray[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CityTableViewController * city = [[CityTableViewController alloc]init];
    city.cityArray = [self.cityArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:city animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
